<?php
namespace app\api\controller;


class UserOrder extends Base
{
  public function lists(){
      $user_id=$this->getuser();
      $page=i("page","请输入页码","int");
      $where=[];
      $where['user_id']=$user_id;
      $where['isused']=1;
      $status=i("status");
      if($status!="all"){
          $where['status']=$status;
      }else{
          $where['status']=['>=',0];
      }
      $lists=model("order")->where($where)->order("time desc")->limit(($page-1)*10,10)->select();
      for($i=0;$i<count($lists);$i++){
          $order=$lists[$i];
          $status=$order['status'];
          $status_str="未知状态";
          switch ($status){
              case 0:$status_str="未付款";break;
              case 1:$status_str="待发货";break;
              case 2:$status_str="派送中";break;
              case 3:$status_str="已完成";break;
              case 4:$status_str="已取消";break;
          }
          $lists[$i]['goods_list']=model("orderGoodsList")->field("order_goods_list.number buynumber,order_goods_list.price price,goods.pic pic,goods.name name,goods.id goods_id")->where("orderno",$order['orderno'])->join("goods","order_goods_list.goods_id=goods.id","LEFT")->select();
          $lists[$i]['status_str']=$status_str;
      }
      return s($lists);
  }

  public function cancel(){
      $user_id=$this->getuser();
      $orderno=i("orderno","请输入订单号");
      $order=model("order")->where("orderno",$orderno)->where("user_id",$user_id)->where("status",0)->find();
      if(empty($order)){
          e("该订单状态不允许取消");
      }
      model("order")->where("orderno",$orderno)->update(["status"=>-1]);
      return s();
  }
  public function confirm(){
      $user_id=$this->getuser();
      $orderno=i("orderno","请输入订单号");
      $order=model("order")->where("orderno",$orderno)->where("user_id",$user_id)->where("status",2)->find();
      if(empty($order)){
          e("该订单状态不允许完成");
      }
      model("order")->where("orderno",$orderno)->update(["status"=>3]);
      return s();
  }
  public function explists(){
      $orderno=i("orderno");
      $order=model("order")->where("orderno",$orderno)->find();
      if(empty($order['exp_no'])||empty($order['exp_order'])){
          e("快递单号不存在");
      }

      $host = "https://wuliu.market.alicloudapi.com";//api访问链接
      $path = "/kdi";//API访问后缀
      $method = "GET";
      $appcode = "4afed0b5c6e3437fbb4320210e410c2c";//替换成自己的阿里云appcode
      $headers = array();
      array_push($headers, "Authorization:APPCODE " . $appcode);
      $querys = "no={$order['exp_order']}&type={$order['exp_no']}";  //参数写在这里
      $bodys = "";
      $url = $host . $path . "?" . $querys;//url拼接

      $curl = curl_init();
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
      curl_setopt($curl, CURLOPT_FAILONERROR, false);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_HEADER, false);
      if (1 == strpos("$".$host, "https://"))
      {
          curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
          curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
      }
      $data=json_decode(curl_exec($curl),true);
      if($data['status']!=0){
          e("快递不存在");
      }

      return s($data['result']['list']);
  }
}
